
add_comma <- function(x, ...) {
  format(x, ..., big.mark = ",", scientific = FALSE, trim = TRUE)
}

load('summary_data/tableA11.rda')
out_rd = clean_names(out_rd)

left_join(
  out_rd,
  out_rd %>%
    group_by(party) %>%
    summarize(tot_by_party = sum(n)) %>%
    ungroup(),
  by = "party"
) %>%
  mutate(pct = (n / tot_by_party) %>% scales::percent(., accuracy = 1, suffix = "\\%")) %>%
  select(party, year2012, year2016, year2020, pct, n) %>% 
  arrange(desc(party), year2012, year2016, year2020) %>%
  mutate(n = add_comma(n)) %>%
  rename(
    Party = party,
    `2012` = year2012,
    `2016` = year2016,
    `2020` = year2020,
    `\\% of Donors` = pct
  ) %>% 
  kableExtra::kbl(
    format   = "latex", 
    caption  = "Percent of Donors Giving in One, Two, or Three Cycles, by Party",
    align    = c(rep("l", 4), rep("r", 2)),
    booktabs = TRUE,
    escape   = FALSE
  ) %>% 
  
  collapse_rows(1, latex_hline = "major") %>%
  kable_styling(full_width = FALSE, font_size = 8) %>%
  save_kable(file = 'tables/tableA11.tex')
